package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@RestController
@Slf4j
@Api(tags = "菜品管理接口")
@RequestMapping("/admin/dish")
public class DishController {

    private final String PATTERN="dish_*";

    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;

    /*
     * 新增菜品
     * */
    @ApiOperation("新增菜品")
    @PostMapping
    public Result addDish(@RequestBody DishDTO dishDTO) {
        log.info("新增菜品:{}", dishDTO.toString());
        dishService.addDish(dishDTO);

        //清理缓存数据
        String key="dish_"+dishDTO.getCategoryId();
        clearCache(key);
        return Result.success();
    }

    /*
     * 菜品分页查询
     * */
    @ApiOperation("菜品分页查询")
    @GetMapping("/page")
    public Result<PageResult> getDishPage(DishPageQueryDTO dishPageQueryDTO) {
        log.info("菜品分页查询:{}", dishPageQueryDTO.toString());
        PageResult pageResult = dishService.getDishPage(dishPageQueryDTO);

        return Result.success(pageResult);
    }

    /*
     * 批量删除菜品
     * */
    @ApiOperation("根据id批量删除菜品")
    @DeleteMapping
    public Result deleteDish(@RequestParam List<Long> ids) {
        log.info("批量删除菜品id:{}", ids.toString());
        dishService.deleteDish(ids);

        //清理菜品缓存数据
        clearCache(PATTERN);

        return Result.success();
    }

    /*
    * 根据id查询菜品信息
    * */
    @ApiOperation("根据id查询菜品信息")
    @GetMapping("/{id}")
    public Result<DishVO> getDishById(@PathVariable Long id) {
        log.info("根据id查询菜品信息:{}",id);
        DishVO data = dishService.getByIdFlavor(id);
        return Result.success(data);
    }
    /*
    * 修改菜品信息
    * */
    @ApiOperation("修改菜品信息")
    @PutMapping
    public Result updateDish(@RequestBody DishDTO dishDTO) {
        log.info("修改菜品:{}", dishDTO.toString());
        dishService.updateDish(dishDTO);

        //清理菜品缓存数据
        clearCache(PATTERN);

        return Result.success();
    }
    /*
    * 起售/停售菜品
    * */
    @ApiOperation("起售/停售菜品")
    @PostMapping("/status/{status}")
    public Result updateDishStatus(@PathVariable Integer status, Long id) {
        log.info("起售/停售菜品:{}{}",status,id);
        dishService.updateDishStatus(status,id);

        //清理菜品缓存数据
        clearCache(PATTERN);

        return Result.success();
    }
    /*
    * 根据分类查询菜品
    * */
    @ApiOperation("根据分类查询菜品")
    @GetMapping("/list")
    public Result<List<Dish>> getDishList(Long categoryId) {
        log.info("根据分类查询菜品:{}",categoryId);
        Dish dish = new Dish();
        dish.setCategoryId(categoryId);
        List<Dish> dishList = dishService.getDishList2(dish);

        return Result.success(dishList);
    }

    /*
    * 清理菜品的缓存数据
    * */
    private void clearCache(String pattern){
        //清理菜品缓存数据
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }
}
